package com.george.shartdingsphere.controller;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.george.shartdingsphere.dao.ProductDao;
import com.george.shartdingsphere.dao.SysUserDao;
import com.george.shartdingsphere.entity.ProductEntity;
import com.george.shartdingsphere.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/demo")
public class DemoController {


    @Autowired
    private ProductDao productDao;

    @Autowired
    private SysUserDao sysUserDao;

//    @DS("dev")
    @DS("shardingDatasource")
    @GetMapping("/product")
    public ProductEntity product(){
        Date now = new Date();
        ProductEntity entity = new ProductEntity();
        entity.setProductNumber(Long.parseLong(RandomUtil.randomNumbers(10)));
        entity.setProductName("产品");
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        productDao.save(entity);
        return entity;
    }

    @DS("shardingDatasource")
    @GetMapping("/getProduct")
    public ProductEntity product(Long number){
        Date now = new Date();
        System.out.println("查询产品,number: " + number);
        ProductEntity entity = productDao.selectByProductNumber(number);
        return entity;
    }

    @GetMapping("/sysUser")
    public SysUserEntity sysUser(){
        Date now = new Date();
        SysUserEntity entity = new SysUserEntity();
        entity.setUserName("用户");
        entity.setAge(20);
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        sysUserDao.save(entity);
        return entity;
    }



}
